class rule:
    def __init__(self,name,rules):
        self.name=name
        self.rules=rules
class knowledgeBase:
    def __init__(self):
        self.rules=[]
    def add_rules(self,r):
        self.rules.append(r)
    def define_rules(self):
        rules=[
            rule("鸟",["有羽毛","会飞"]),
            rule("企鹅",["有羽毛","不会飞"]),
            rule("鱼",["有鳞片","生活在水中"]),
            rule("狮子",["会吼叫","是哺乳动物"]),
            rule("猴子",["是哺乳动物","喜欢攀爬"]),
        ]
        for r in rules:
            self.add_rules(r)
class inferenceEngine:
    def __init__(self):
        self.explanation=[]
        self.konwledge=knowledgeBase()
        self.konwledge.define_rules()
        self.result=[]
        self.question=["有羽毛","会飞","有鳞片","生活在水中","是哺乳动物","会吼叫","喜欢攀爬"]
        for i in self.konwledge.rules:
            self.result.append(i.name)
    def rm(self,qu,an):
        re=[]
        if(an=="是"):
            for i in self.result:
                for j in self.konwledge.rules:
                    if(i==j.name):
                        if(qu in j.rules):
                            re.append(i)
                        break
        else:
            for i in self.result:
                for j in self.konwledge.rules:
                    if(i==j.name):
                        if(qu not in j.rules):
                            re.append(i)
                        break
        self.result=re
        print(self.result)
    def diagnose(self):
        for i in self.question:
            print(i+"吗？")
            str=input()
            self.explanation.append(i+"吗？"+str)
            self.rm(i,str)
            if(len(self.result)<=1):
                break
def main():
    while(1):
        print("欢迎来到使用动物识别专家系统！请回答一下问题（是/否）：",end='\n')
        infer=inferenceEngine()
        infer.diagnose()
        if(infer.result is None):
            print("系统无法推断该动物！")
        else:
            print("系统推断动物是："+infer.result[0])
        print("推理过程：")
        for i in infer.explanation:
            print("-"+i)
main()


   
                    

